﻿<%@ Control Language="C#" AutoEventWireup="true" CodeFile="LoanStatus.ascx.cs" Inherits="Manager_Lib_Controls_StateFlow_LoanStatus" %>
<DS:StateFlowNextStatesDataSource ID="dsNextStates" runat="server">
<SelectParameters>
	<asp:Parameter Name="StateFromID" DefaultValue="0" Type="Int32" />
</SelectParameters>
</DS:StateFlowNextStatesDataSource>
<%--<DS:StatesDataSource TableName="t_Collection" Access="Add" EmtyRowPolicy="First" SelectList="ID StateToID,Name StateTo" id="dsState" runat="server" />
--%>
<UI:PlaceHolderJS runat="server">
<asp:ListBox id="sel" dataSrc="#XmlLoanInfo" datafld="StatusID" wsMethod=""
						 onbeforeupdate="this.onbeforeupdateFired=true;this.valuePrev=rsField(this,this.dataFld).value"
						 onchange="beforeStatusChange(this,UF_Status)"
						 style="font-family:menu;visibility:visible" OnDataBound="sel_DataBound"
						 DataSourceID="dsNextStates" DataTextField="StateTo" DataValueField="StateToID" runat="server" />
</UI:PlaceHolderJS>
<script>
if( typeof(<%=Page.Form.ID%>)!='undefined')resizeSelect(<%=Page.Form.ID%>.sel<%=WSMethod%>);//mainForm.selLoanStatus.selectedIndex = mainForm.selLoanStatus.length-1;
var UF_<%=WSMethod%> = UF_AddCallBacks("UF_<%=WSMethod%>",Do<%=WSMethod%>); UF_<%=WSMethod%>.passTrough = true;
if( typeof(<%=Page.Form.ID%>)!='undefined')var sel<%=WSMethod%> = <%=Page.Form.ID%>.sel<%=WSMethod%>;

if( typeof(statusCallbacks) == 'undefined' )statusCallbacks = [];
if( !Array.contains(statusCallbacks,Do<%=WSMethod%>ListRefresh) )
	statusCallbacks.push(Do<%=WSMethod%>ListRefresh);
function Do<%=WSMethod%>(ret){
var selName = "sel<%=WSMethod%>";
	var sel = typeof($get) == 'undefined' ? sel<%=WSMethod%> : $get(selName);
	var NEXT_STATUSES_TABLE = "NextStatuses";
	var rsRet;
	if( ret.error ){
		var message = ret.errorDetail.string.match(reXmlMessage) || htmlDecode(ret.errorDetail.string).match(reXmlMessage);
		if( !message )return;
		rsRet = getRSFromString(message[0]);
	}else	rsRet = getRSFromString(ret.value);
	fillSelect(rsRet.Fields(NEXT_STATUSES_TABLE).value,$get(selName),"StateTo","StateToID");
	//,XmlLoanInfo.recordset.Fields("LoanStatusID").value);
	var loanXML = getDSFromRS(rsRet.Fields("LoanInfo").value).xml;
	<%=dataSrc%>.loadXML("");
	<%=dataSrc%>.loadXML(loanXML);
	resizeSelect($get(selName));
	if( typeof(reloadPage) != 'undefined' )try{ reloadPage();}catch(e){}
	if( $get(selName).inEvent )return;
	fireEvents(selName,ret);
		/// Locals
	function fireEvents(selNameChanged,ret){
		fireOnReloadVerTree({ID:ApplID(),reload:true});
		fireListRefresh(ApplID);
		if( typeof(events) != 'undefined' )events.reloadVerTree();
		Array.forEach(statusCallbacks,function(cb){
			cb(selNameChanged,ret);
		});
	}
}
function Do<%=WSMethod%>ListRefresh(selNameChanged,ret){
	var selName = "sel<%=WSMethod%>";
	if( selName == selNameChanged)return;
//	$get(selName).valuePrev = $get(selName).value;
	$get(selName).inEvent = true;
	$get(selName).onchange();
	$get(selName).inEvent = false;
}
function beforeStatusChange(sel,UF_Status){
	if(!sel.onbeforeupdateFired)	sel.valuePrev=rsField(sel,sel.dataFld).value;
	if(!sel.wsMethod)return alert('wdMethod is empty.');
	if(!sel.inEvent && !confirm('Change '+sel.dataFld.replace('StatusID_','')+' Status?')){
		sel.value = rsField(sel,sel.dataFld).value;
		return false;
	}else{
		return UF_Status(sel,sel.inEvent?"0":sel.value,sel.valuePrev,sel.wsMethod);
		}
}
</script>